﻿using QuanLyGiaoVuDAL.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyGiaoVuDAL.Mapper
{
    public static class DatarowMapper
    {
        #region GiangVien
        public static GiangVienDTO ToGiangVienDTO(this DataRow dataRow)
        {
            GiangVienDTO giangvien = new GiangVienDTO();
            giangvien.ID = Guid.Parse(dataRow["ID"].ToString());
            giangvien.MaGiangVien = dataRow["MaGiangVien"].ToString();
            giangvien.HoTenGV = dataRow["HoTenGV"].ToString();
            giangvien.GioiTinh = (bool)(dataRow["GioiTinh"]) ? 1:0;
            DateTime ngaySinh;
            giangvien.NgaySinh = DateTime.TryParse(dataRow["NgaySinh"].ToString(), out ngaySinh) ? (DateTime?)ngaySinh : null;
            giangvien.NgayVaoCongTac = DateTime.Parse(dataRow["NgayVaoCongTac"].ToString());
            int maBoMon;
            giangvien.MaBoMon = Int32.TryParse(dataRow["BoMon"].ToString(), out maBoMon) ? (int?)maBoMon : null;
            if (giangvien.MaBoMon != null)
            {
                BoMonDTO boMon = new BoMonDTO();
                boMon.MaBoMon = giangvien.MaBoMon.Value;
                boMon.TenBoMon = dataRow["TenBoMon"].ToString();
                giangvien.BoMon = boMon;
            }

            giangvien.MaHocVi = Convert.ToInt32(dataRow["MaHocVi"]);
            HocViDTO hocviDTO = new HocViDTO();
            hocviDTO.MaHocVi = giangvien.MaHocVi;
            hocviDTO.TenHocVi = dataRow["TenHocVi"].ToString();
            hocviDTO.VietTat = dataRow["HocViVT"].ToString();
            giangvien.HocVi = hocviDTO;
            int maHocHam;
            giangvien.MaHocHam = Int32.TryParse(dataRow["MaHocHam"].ToString(), out maHocHam) ? (int?)maHocHam : null;

            if (giangvien.MaHocHam != null)
            {
                HocHamDTO hocHam = new HocHamDTO();
                hocHam.MaHocHam = (int)giangvien.MaHocHam;
                hocHam.TenHocHam = dataRow["TenHocHam"].ToString();
                hocHam.VietTat = dataRow["HocHamVT"].ToString();
                giangvien.HocHam = hocHam;
            }
            else
                giangvien.HocHam = null;
            
            Guid maChucDanh;
            giangvien.MaChucDanh = Guid.TryParse(dataRow["MaChucDanh"].ToString(),out maChucDanh)==true? (Guid?)maChucDanh : null;
           if (giangvien.MaChucDanh != null)
           {
               ChucDanhDTO chucDanhDTO = new ChucDanhDTO();
               chucDanhDTO.MaChucDanh = maChucDanh;
               chucDanhDTO.TenChucDanh = dataRow["TenChucDanh"].ToString();
               chucDanhDTO.GioChuanCacMon=Int32.Parse(dataRow["GioChuanCacMon"].ToString());
               chucDanhDTO.VietTat = dataRow["ChucDanhVT"].ToString();
               giangvien.ChucDanh = chucDanhDTO;
           }
            giangvien.DiaChi = dataRow["DiaChi"].ToString();
            giangvien.SoDienThoai = dataRow["SoDienThoai"].ToString();
            giangvien.Email = dataRow["Email"].ToString();
            giangvien.ThinhGiang = Convert.ToBoolean(dataRow["ThinhGiang"]);
            return giangvien;
        }

        public static HocViDTO ToHocViDTO(this DataRow dataRow)
        {
            HocViDTO hocviDTO = new HocViDTO();
            hocviDTO.MaHocVi = Int32.Parse(dataRow["MaHocVi"].ToString());
            hocviDTO.TenHocVi = dataRow["TenHocVi"].ToString();
            hocviDTO.VietTat = dataRow["VietTat"].ToString();
            return hocviDTO;
        }

        public static HocHamDTO ToHocHamDTO(this DataRow dataRow)
        {
            HocHamDTO hocHamDTO = new HocHamDTO();
            hocHamDTO.MaHocHam = Int32.Parse(dataRow["MaHocHam"].ToString());
            hocHamDTO.TenHocHam = dataRow["TenHocHam"].ToString();
            hocHamDTO.VietTat = dataRow["VietTat"].ToString();
            return hocHamDTO;
        }

        public static ChucVuDTO ToChucVuDTO(this DataRow dataRow)
        {
            ChucVuDTO ChucVuDTO = new ChucVuDTO();
            ChucVuDTO.MaChucVu = Guid.Parse(dataRow["MaChucVu"].ToString());
            ChucVuDTO.TenChucVu = dataRow["TenChucVu"].ToString();
            ChucVuDTO.ThongTinChucVu = dataRow["ThongTinChucVu"].ToString();
            return ChucVuDTO;
        }
        public static ChucVuDTO ToGiangVienChucVuDTO(this DataRow dataRow)
        {
            ChucVuDTO ChucVuDTO = new ChucVuDTO();
            ChucVuDTO.MaChucVu = Guid.Parse(dataRow["MaChucVu"].ToString());
            ChucVuDTO.TenChucVu = dataRow["TenChucVu"].ToString();
            ChucVuDTO.ThongTinChucVu = dataRow["ThongTinChucVu"].ToString();
            ChucVuDTO.TiLeGiam = Int32.Parse(dataRow["TiLeGiam"].ToString());

            return ChucVuDTO;
        }
        public static GiangVienDTO ParseToGiangVienDTO(this DataRow dataRow)
        {
            try
            {
                GiangVienDTO giangvien = new GiangVienDTO();
                giangvien.ID = Guid.Parse(dataRow["giangVienID"].ToString());
                giangvien.MaGiangVien = dataRow["MaGiangVien"].ToString();
                giangvien.HoTenGV = dataRow["HoTenGV"].ToString();
                giangvien.GioiTinh = (bool)(dataRow["GioiTinh"]) ? 1 : 0;
                DateTime ngaySinh;
                giangvien.NgaySinh = DateTime.TryParse(dataRow["NgaySinh"].ToString(), out ngaySinh) ? (DateTime?)ngaySinh : null;
                giangvien.NgayVaoCongTac = DateTime.Parse(dataRow["NgayVaoCongTac"].ToString());
                giangvien.DaXoa = Convert.ToBoolean(dataRow["DaXoa"]);
                giangvien.MaHocVi = Convert.ToInt32(dataRow["MaHocVi"]);
                HocViDTO hocviDTO = new HocViDTO();
                hocviDTO.MaHocVi = giangvien.MaHocVi;
                hocviDTO.TenHocVi = dataRow["TenHocVi"].ToString();
                hocviDTO.VietTat = dataRow["HocViVT"].ToString();
                giangvien.HocVi = hocviDTO;
                int maHocHam;
                giangvien.MaHocHam = Int32.TryParse(dataRow["MaHocHam"].ToString(), out maHocHam) ? (int?)maHocHam : null;

                if (giangvien.MaHocHam != null)
                {
                    HocHamDTO hocHam = new HocHamDTO();
                    hocHam.MaHocHam = (int)giangvien.MaHocHam;
                    hocHam.TenHocHam = dataRow["TenHocHam"].ToString();
                    hocHam.VietTat = dataRow["HocHamVT"].ToString();
                    giangvien.HocHam = hocHam;
                }
                else
                    giangvien.HocHam = null;

                Guid maChucDanh;
                giangvien.MaChucDanh = Guid.TryParse(dataRow["MaChucDanh"].ToString(), out maChucDanh) == true ? (Guid?)maChucDanh : null;
                if (giangvien.MaChucDanh != null)
                {
                    ChucDanhDTO chucDanhDTO = new ChucDanhDTO();
                    chucDanhDTO.MaChucDanh = maChucDanh;
                    chucDanhDTO.TenChucDanh = dataRow["TenChucDanh"].ToString();
                    chucDanhDTO.GioChuanCacMon = Int32.Parse(dataRow["GioChuanCacMon"].ToString());
                    chucDanhDTO.VietTat = dataRow["ChucDanhVT"].ToString();

                    giangvien.ChucDanh = chucDanhDTO;
                }
                else
                    giangvien.ChucDanh = null;

                giangvien.DiaChi = dataRow["DiaChi"].ToString();
                giangvien.SoDienThoai = dataRow["SoDienThoai"].ToString();
                giangvien.Email = dataRow["Email"].ToString();
                return giangvien;
            }
            catch (Exception exception)
            {
                return null;
            }
        }

        public static ChucDanhDTO ToChucDanhDTO(this DataRow dataRow)
        {
            ChucDanhDTO chucDanhDTO = new ChucDanhDTO();
            chucDanhDTO.MaChucDanh = Guid.Parse(dataRow["MaChucDanh"].ToString());
            chucDanhDTO.TenChucDanh = dataRow["TenChucDanh"].ToString();
            chucDanhDTO.GioChuanCacMon = Int32.Parse(dataRow["GioChuanCacMon"].ToString());
            chucDanhDTO.VietTat = dataRow["VietTat"].ToString();
            return chucDanhDTO;
        }

        public static LichRanhDTO ToLichRanhDTO(this DataRow dataRow)
        {
            LichRanhDTO lichRanhDTO = new LichRanhDTO();
            lichRanhDTO.MaLichRanh = Guid.Parse(dataRow["MaLichRanh"].ToString());
            lichRanhDTO.GiangVien = Guid.Parse(dataRow["GiangVienId"].ToString());
            lichRanhDTO.Buoi = Convert.ToBoolean(dataRow["Buoi"]);
            lichRanhDTO.Thu = Convert.ToInt32(dataRow["Thu"]);
            return lichRanhDTO;
        }

        public static BoMonDTO ToBoMonDTO(this DataRow dataRow)
        {
            BoMonDTO boMonDTO = new BoMonDTO();
            boMonDTO.MaBoMon = Convert.ToInt32(dataRow["MaBoMon"]);
            boMonDTO.TenBoMon = dataRow["TenBoMon"].ToString();
            return boMonDTO;
        }

        #endregion

        #region MonHoc
        public static MonHocDTO ToMonHocDTO(this DataRow dataRow)
        {
            MonHocDTO monHocDTO = new MonHocDTO();
            monHocDTO.ID = Guid.Parse(dataRow["ID"].ToString());
            monHocDTO.MaMonHoc = dataRow["MaMonHoc"].ToString();
            monHocDTO.TenMonHoc = dataRow["TenMonHoc"].ToString();
            monHocDTO.TongSoTinChi = Convert.ToInt16(dataRow["SoTinChi"]);
            monHocDTO.SoTinChiLyThuyet = Convert.ToInt16(dataRow["SoTinChiLyThuyet"]);
            short hocKy;
            monHocDTO.HocKy = Int16.TryParse(dataRow["HocKy"].ToString(), out hocKy) ? hocKy : (short)0;
            Guid maMonChinh;
            monHocDTO.MaMonChinh = Guid.TryParse(dataRow["MaMonChinh"].ToString(), out maMonChinh) == true ? ((Guid?)maMonChinh) : null;

            if (monHocDTO.MaMonChinh != null)
            {
                MonHocDTO monHocChinh = new MonHocDTO();
                monHocChinh.ID = (Guid)monHocDTO.MaMonChinh;
                monHocChinh.MaMonHoc = dataRow["MaMonHocChinh"].ToString();
                monHocChinh.TenMonHoc = dataRow["TenMonHocChinh"].ToString();
                monHocDTO.MonHocChinh = monHocChinh;
            }

            return monHocDTO;
        }

        public static MonHocDTO ParseKhaNangToMonHocDTO(this DataRow dataRow)
        {
            MonHocDTO monHocDTO = new MonHocDTO();
            monHocDTO.ID = Guid.Parse(dataRow["monHocID"].ToString());
            monHocDTO.MaMonHoc = dataRow["MaMonHoc"].ToString();
            monHocDTO.TenMonHoc = dataRow["TenMonHoc"].ToString();
            monHocDTO.TongSoTinChi = Convert.ToInt16(dataRow["SoTinChi"]);
            monHocDTO.SoTinChiLyThuyet = Convert.ToInt16(dataRow["SoTinChiLyThuyet"]);
            monHocDTO.HocKy = Convert.ToInt16(dataRow["HocKy"]);
            Guid maMonChinh;
            monHocDTO.MaMonChinh = Guid.TryParse(dataRow["MaMonChinh"].ToString(), out maMonChinh) == true ? ((Guid?)maMonChinh) : null;

            if (monHocDTO.MaMonChinh != null)
            {
                MonHocDTO monHocChinh = new MonHocDTO();
                monHocChinh.ID = (Guid)monHocDTO.MaMonChinh;
                monHocChinh.MaMonHoc = dataRow["MaMonHocChinh"].ToString();
                monHocChinh.TenMonHoc = dataRow["TenMonHocChinh"].ToString();
                monHocDTO.MonHocChinh = monHocChinh;
            }

            return monHocDTO;
        }

        public static MonHocDTO ParseCTBoMonToMonHocDTO(this DataRow dataRow) {
            MonHocDTO monHoc = new MonHocDTO();
            monHoc.ID = Guid.Parse(dataRow["monHocID"].ToString());
            monHoc.MaMonHoc = dataRow["MaMonHoc"].ToString();
            monHoc.TenMonHoc = dataRow["TenMonHoc"].ToString();
            monHoc.TongSoTinChi = Convert.ToInt16(dataRow["SoTinChi"]);
            monHoc.SoTinChiLyThuyet = Convert.ToInt16(dataRow["SoTinChiLyThuyet"]);
            return monHoc;
        }
        #endregion

        #region HocKy
        public static HocKyDTO ToHocKyDTO(this DataRow dataRow)
        {
            HocKyDTO hocKyDTO = new HocKyDTO();
            hocKyDTO.MaHocKy = Guid.Parse(dataRow["MaHocKy"].ToString());
            hocKyDTO.TenHocKy = Convert.ToInt32(dataRow["TenHocKy"]);
            hocKyDTO.MaNamHoc = Guid.Parse(dataRow["MaNamHoc"].ToString());
            NamHocDTO namHoc = new NamHocDTO();
            namHoc.MaNamHoc = hocKyDTO.MaNamHoc;
            namHoc.Nam1 = Convert.ToInt16(dataRow["Nam1"].ToString());
            namHoc.Nam2 = Convert.ToInt16(dataRow["Nam2"].ToString());
            hocKyDTO.NamHoc = namHoc;
            return hocKyDTO;
        }

        public static NamHocDTO ToNamHocDTO(this DataRow dataRow)
        {
            NamHocDTO namHocDTO = new NamHocDTO();
            namHocDTO.MaNamHoc = Guid.Parse(dataRow["MaNamHoc"].ToString());
            namHocDTO.Nam1 = Convert.ToInt32(dataRow["Nam1"]);
            namHocDTO.Nam2 = Convert.ToInt32(dataRow["Nam2"]);
            return namHocDTO;
        }
        #endregion

        #region LopMonHoc
        public static LopMonHocDTO ToLopMonHocDTO(this DataRow dataRow)
        {
            LopMonHocDTO lopMonHoc = new LopMonHocDTO();
            lopMonHoc.LopMonHocId = Guid.Parse(dataRow["LopMonHocId"].ToString());
            lopMonHoc.MaLopMonHoc = dataRow["MaLopMonHoc"].ToString();
            lopMonHoc.MaMonHoc = Guid.Parse(dataRow["MonHocID"].ToString());
            lopMonHoc.MaGiangVien1 = Guid.Parse(dataRow["GiangVien1ID"].ToString());
            Guid giangVien2Id;
            lopMonHoc.MaGiangVien2 = Guid.TryParse(dataRow["GiangVien2ID"].ToString(), out giangVien2Id) ? (Guid?)giangVien2Id : null;
            lopMonHoc.MaHocKy = Guid.Parse(dataRow["MaHocKy"].ToString());
            int maTietDau;
            lopMonHoc.MaTietDau = Int32.TryParse(dataRow["MaTietDau"].ToString(), out maTietDau) ? (int?)maTietDau : null;
            int maTietSau;
            lopMonHoc.MaTietSau = Int32.TryParse(dataRow["MaTietSau"].ToString(), out maTietSau) ? (int?)maTietSau : null;
            lopMonHoc.Thu = Int16.Parse(dataRow["Thu"].ToString());
            Guid maPhong;
            lopMonHoc.MaPhong = Guid.TryParse(dataRow["MaPhong"].ToString(), out maPhong) ? (Guid?)maPhong : null;
            int siSo;
            lopMonHoc.SiSoToiDa = Int32.TryParse(dataRow["SiSoToiDa"].ToString(), out siSo) ? siSo : 0;
            lopMonHoc.SiSoThucTe = Int32.TryParse(dataRow["SiSoThucTe"].ToString(), out siSo) ? siSo : 0;

            lopMonHoc.LoaiLop = Convert.ToInt32(dataRow["LoaiLop"]);
            lopMonHoc.TyLeDiem = Int16.Parse(dataRow["TyLeDiem"].ToString());
            Guid maLopChinh;
            lopMonHoc.MaLopChinh = Guid.TryParse(dataRow["LopMonHocChinhId"].ToString(), out maLopChinh) ? (Guid?)maLopChinh : null;
            Guid maLopSV;
            lopMonHoc.LopSinhVienId = Guid.TryParse(dataRow["LopSinhVienId"].ToString(), out maLopSV) ? (Guid?)maLopSV : null;
            lopMonHoc.GiangVienXacNhan = Convert.ToBoolean(dataRow["GiangVienXacNhan"]);
            lopMonHoc.DaPhanHoi = Convert.ToBoolean(dataRow["DaPhanHoi"]);
            lopMonHoc.GhiChu = dataRow["GhiChu"].ToString();
            lopMonHoc.DongYMoLop = Convert.ToBoolean(dataRow["DongYMoLop"]);
            DateTime buoi;
            lopMonHoc.Buoi1 = DateTime.TryParse(dataRow["Buoi1"].ToString(), out buoi) ? (DateTime?)buoi : null;
            lopMonHoc.Buoi2 = DateTime.TryParse(dataRow["Buoi2"].ToString(), out buoi) ? (DateTime?)buoi : null;
            lopMonHoc.Buoi3 = DateTime.TryParse(dataRow["Buoi3"].ToString(), out buoi) ? (DateTime?)buoi : null;

            //Fill data for objects in Lopmonhoc
            MonHocDTO monhoc = new MonHocDTO();
            monhoc.ID = lopMonHoc.MaMonHoc;
            monhoc.MaMonHoc = dataRow["MaMonHoc"].ToString();
            monhoc.TenMonHoc = dataRow["TenMonHoc"].ToString();
            monhoc.TongSoTinChi = Convert.ToInt16(dataRow["SoTinChi"]);
            monhoc.SoTinChiLyThuyet = Convert.ToInt16(dataRow["SoTinChiLyThuyet"]);
            lopMonHoc.MonHoc = monhoc;

            GiangVienDTO giangVien1 = new GiangVienDTO();
            giangVien1.ID = lopMonHoc.MaGiangVien1;
            giangVien1.HoTenGV = dataRow["HoTenGV1"].ToString();
            giangVien1.ThinhGiang = Convert.ToBoolean(dataRow["ThinhGiang1"]);
            giangVien1.MaHocVi = Convert.ToInt32(dataRow["MaHocVi"]);

            HocViDTO hocVi = new HocViDTO();
            hocVi.MaHocVi = giangVien1.MaHocVi;
            hocVi.TenHocVi = dataRow["TenHocVi"].ToString();
            hocVi.VietTat = dataRow["HocViVT"].ToString();
            giangVien1.HocVi = hocVi;

            int maHocHam;
            giangVien1.MaHocHam = Int32.TryParse(dataRow["MaHocHam"].ToString(), out maHocHam) ? (int?)maHocHam : null;
            if (giangVien1.MaHocHam != null)
            {
                HocHamDTO hocHam = new HocHamDTO();
                hocHam.MaHocHam = (Int32)giangVien1.MaHocHam;
                hocHam.TenHocHam = dataRow["TenHocHam"].ToString();
                hocHam.VietTat = dataRow["HocHamVT"].ToString();
                giangVien1.HocHam = hocHam;
            }
            else
                giangVien1.HocHam = null;

            lopMonHoc.GiangVien1 = giangVien1;

            if (lopMonHoc.MaGiangVien2 != null)
            {
                GiangVienDTO giangVien2 = new GiangVienDTO();
                giangVien2.ID = lopMonHoc.MaGiangVien2.Value;
                giangVien2.HoTenGV = dataRow["HoTenGV2"].ToString();
                giangVien2.MaHocVi = Convert.ToInt32(dataRow["MaHocVi2"]);

                HocViDTO hocVi2 = new HocViDTO();
                hocVi2.MaHocVi = giangVien2.MaHocVi;
                hocVi2.TenHocVi = dataRow["TenHocVi2"].ToString();
                hocVi2.VietTat = dataRow["HocViVT2"].ToString();
                giangVien2.HocVi = hocVi2;

                giangVien2.MaHocHam = Int32.TryParse(dataRow["MaHocHam2"].ToString(), out maHocHam) ? (int?)maHocHam : null;
                if (giangVien2.MaHocHam != null)
                {
                    HocHamDTO hocHam = new HocHamDTO();
                    hocHam.MaHocHam = (Int32)giangVien2.MaHocHam;
                    hocHam.TenHocHam = dataRow["TenHocHam2"].ToString();
                    hocHam.VietTat = dataRow["HocHamVT2"].ToString();
                    giangVien2.HocHam = hocHam;
                }
                else
                    giangVien2.HocHam = null;

                lopMonHoc.GiangVien2 = giangVien2;
            }
            

            HocKyDTO hocky = new HocKyDTO();
            hocky.MaHocKy = lopMonHoc.MaHocKy;
            hocky.TenHocKy = Convert.ToInt32(dataRow["TenHocKy"]);
            hocky.MaNamHoc = Guid.Parse(dataRow["MaNamHoc"].ToString());

            NamHocDTO namHoc = new NamHocDTO();
            namHoc.MaNamHoc = hocky.MaNamHoc;
            namHoc.Nam1 = Convert.ToInt32(dataRow["nam1"]);
            namHoc.Nam2 = Convert.ToInt32(dataRow["nam2"]);
            hocky.NamHoc = namHoc;
            lopMonHoc.HocKy = hocky;

            if (lopMonHoc.MaTietDau != null)
            {
                TietHocDTO tietHocDau = new TietHocDTO();
                tietHocDau.MaTiet = (int)lopMonHoc.MaTietDau;
                tietHocDau.TietThu = Convert.ToInt32(dataRow["TietDau"]);
                tietHocDau.GioBatDau = TimeSpan.Parse(dataRow["GioBatDauTietDau"].ToString());
                tietHocDau.GioKetThuc = TimeSpan.Parse(dataRow["GioKetThucTietDau"].ToString());
                tietHocDau.Ca = Convert.ToInt32(dataRow["CaDau"]);
                tietHocDau.Buoi = Convert.ToBoolean(dataRow["BuoiDau"]);
                lopMonHoc.TietDau = tietHocDau;
            }
            else lopMonHoc.TietDau = null;

            if (lopMonHoc.MaTietSau != null)
            {
                TietHocDTO tietHocSau = new TietHocDTO();
                tietHocSau.MaTiet = (int)lopMonHoc.MaTietSau;
                tietHocSau.TietThu = Convert.ToInt32(dataRow["TietSau"]);
                tietHocSau.GioBatDau = TimeSpan.Parse(dataRow["GioBatDauTietDau"].ToString());
                tietHocSau.GioKetThuc = TimeSpan.Parse(dataRow["GioKetThucTietDau"].ToString());
                tietHocSau.Ca = Convert.ToInt32(dataRow["CaSau"]);
                tietHocSau.Buoi = Convert.ToBoolean(dataRow["BuoiSau"]);
                lopMonHoc.TietSau = tietHocSau;
            }
            else lopMonHoc.TietSau = null;

            if (lopMonHoc.LopSinhVienId != null)
            {
                LopSinhVienDTO lopSV = new LopSinhVienDTO();
                lopSV.MaLop = (Guid)lopMonHoc.LopSinhVienId;
                lopSV.TenLop = dataRow["TenLopSV"].ToString();
                lopSV.KhoaNam = Convert.ToInt32(dataRow["KhoaNam"]);
                lopSV.HeDaoTao = dataRow["HeDaoTao"].ToString();
                lopMonHoc.LopSinhVien = lopSV;
            }
            else
                lopMonHoc.LopSinhVien = null;

            if (lopMonHoc.MaLopChinh != null && lopMonHoc.MaLopChinh != Guid.Empty)
            {
                LopMonHocDTO lopMonHocChinh = new LopMonHocDTO();
                lopMonHocChinh.LopMonHocId = lopMonHoc.MaLopChinh.Value;
                lopMonHocChinh.MaLopMonHoc = dataRow["MaLopMonHocChinh"].ToString();
                lopMonHocChinh.MaMonHoc = lopMonHoc.MaMonHoc;
                lopMonHocChinh.MonHoc = lopMonHoc.MonHoc;
                lopMonHoc.LopChinh = lopMonHocChinh;
            }
            else
                lopMonHoc.LopChinh = null;

            lopMonHoc.Phong = dataRow["Phong"].ToString();

            return lopMonHoc;
        }

        public static LoaiLopDTO ToLoaiLopMonHocDTO(this DataRow dataRow)
        {
            LoaiLopDTO loaiLopMonHoc = new LoaiLopDTO();
            loaiLopMonHoc.MaLoaiLopMonHoc = Guid.Parse(dataRow["MaLoaiLopMonHoc"].ToString());
            loaiLopMonHoc.TenLoaiLopMonHoc = dataRow["TenLoaiLopMonHoc"].ToString();
            return loaiLopMonHoc;
        }

        public static LopSinhVienDTO ToLopSinhVienDTO(this DataRow dataRow)
        {
            LopSinhVienDTO lopSV = new LopSinhVienDTO();
            lopSV.MaLop = Guid.Parse(dataRow["MaLop"].ToString());
            lopSV.TenLop = dataRow["TenLop"].ToString();
            lopSV.KhoaNam = Convert.ToInt32(dataRow["KhoaNam"]);
            lopSV.HeDaoTao = dataRow["HeDaoTao"].ToString();
            lopSV.GiangVienId = Guid.Parse(dataRow["GiangVienId"].ToString());
            return lopSV;
        }
        #endregion

        #region Ca
        public static CaDTO ToCaDTO(this DataRow dataRow)
        {
            CaDTO cadto = new CaDTO();
            cadto.MaCa = Guid.Parse(dataRow["MaCa"].ToString());
            cadto.TenCa = Convert.ToInt16(dataRow["TenCa"]);
            cadto.ThoiGianBatDau = Convert.ToDateTime(dataRow["ThoiGianBatDau"]);
            cadto.ThoiGianKetThuc = Convert.ToDateTime(dataRow["ThoiGianKetThuc"]);
            cadto.TietBatDau = Convert.ToInt16(dataRow["TietBatDau"]);
            cadto.TietKetThuc = Convert.ToInt16(dataRow["TietKetThuc"]);
            return cadto;
        }
        #endregion

        #region TietHoc
        public static TietHocDTO ToTietHocDTO(this DataRow dataRow)
        {
            TietHocDTO tietHocDTO = new TietHocDTO();
            tietHocDTO.MaTiet = Convert.ToInt32(dataRow["MaTiet"]);
            tietHocDTO.TietThu = Convert.ToInt32(dataRow["Tiet"]);
            tietHocDTO.GioBatDau = TimeSpan.Parse(dataRow["GioBatDau"].ToString());
            tietHocDTO.GioKetThuc = TimeSpan.Parse(dataRow["GioKetThuc"].ToString());
            int ca;
            tietHocDTO.Ca = Int32.TryParse(dataRow["Ca"].ToString(), out ca) ? (int?)ca: null;
            tietHocDTO.Buoi = Convert.ToBoolean(dataRow["Buoi"]);
            return tietHocDTO;
        }
        #endregion

        #region ThongTinDangKyGioDay
        public static DangKyGioDayDTO ToDangKyGioDayDTO(this DataRow dataRow)
        {
            DangKyGioDayDTO dangKyGioDayDTO = new DangKyGioDayDTO();
            dangKyGioDayDTO.MaThongTinGiangDay = Guid.Parse(dataRow["MaThongTinGiangDay"].ToString());
            dangKyGioDayDTO.GiangVienId = Guid.Parse(dataRow["GiangVienId"].ToString());

            GiangVienDTO giangVienDTO = new GiangVienDTO();
            giangVienDTO.ID = dangKyGioDayDTO.GiangVienId;
            giangVienDTO.HoTenGV = dataRow["HoTenGV"].ToString();
            DateTime ngaySinh;
            giangVienDTO.NgaySinh = DateTime.TryParse(dataRow["NgaySinh"].ToString(), out ngaySinh) ? (DateTime?)ngaySinh : null;
            giangVienDTO.GioiTinh = (bool)(dataRow["GioiTinh"]) ? 1 : 0;
            
            giangVienDTO.NgayVaoCongTac = DateTime.Parse(dataRow["NgayVaoCongTac"].ToString());
            giangVienDTO.MaHocVi = Convert.ToInt32(dataRow["MaHocVi"]);
            HocViDTO hocviDTO = new HocViDTO();
            hocviDTO.MaHocVi = giangVienDTO.MaHocVi;
            hocviDTO.TenHocVi = dataRow["TenHocVi"].ToString();
            hocviDTO.VietTat = dataRow["HocViVT"].ToString();
            giangVienDTO.HocVi = hocviDTO;
            int maHocHam;
            giangVienDTO.MaHocHam = Int32.TryParse(dataRow["MaHocHam"].ToString(), out maHocHam) ? (int?)maHocHam : null;

            if (giangVienDTO.MaHocHam != null)
            {
                HocHamDTO hocHam = new HocHamDTO();
                hocHam.MaHocHam = (int)giangVienDTO.MaHocHam;
                hocHam.TenHocHam = dataRow["TenHocHam"].ToString();
                hocHam.VietTat = dataRow["HocHamVT"].ToString();
                giangVienDTO.HocHam = hocHam;
            }
            else
                giangVienDTO.HocHam = null;

            dangKyGioDayDTO.GiangVienDTO = giangVienDTO;

            dangKyGioDayDTO.MaLopMonHoc = Guid.Parse(dataRow["LopMonHocId"].ToString());

            LopMonHocDTO lopMonHocDTO = new LopMonHocDTO();
            lopMonHocDTO.LopMonHocId = dangKyGioDayDTO.MaLopMonHoc;
            lopMonHocDTO.MaMonHoc = Guid.Parse(dataRow["MonHocId"].ToString());
            lopMonHocDTO.MaLopMonHoc = dataRow["MaLopMonHoc"].ToString();
            Guid lopSinhVienId;
            lopMonHocDTO.LopSinhVienId = Guid.TryParse(dataRow["LopSinhVienId"].ToString(), out lopSinhVienId) ? (Guid?)lopSinhVienId : null;
            if (lopMonHocDTO.LopSinhVienId != null)
            {
                LopSinhVienDTO lopSinhVien = new LopSinhVienDTO();
                lopSinhVien.MaLop = (Guid)lopMonHocDTO.LopSinhVienId;
                lopSinhVien.TenLop = dataRow["TenLopSV"].ToString();
                lopSinhVien.KhoaNam = Convert.ToInt32(dataRow["KhoaNam"]);
                lopSinhVien.HeDaoTao = dataRow["HeDaoTao"].ToString();
                lopMonHocDTO.LopSinhVien = lopSinhVien;
            }
            
            MonHocDTO monHocDTO = new MonHocDTO();
            monHocDTO.ID = lopMonHocDTO.MaMonHoc;
            monHocDTO.MaMonHoc = dataRow["MaMonHoc"].ToString();
            monHocDTO.TenMonHoc = dataRow["TenMonHoc"].ToString();
            monHocDTO.TongSoTinChi = Convert.ToInt16(dataRow["SoTinChi"]);
            monHocDTO.SoTinChiLyThuyet = Convert.ToInt16(dataRow["SoTinChiLyThuyet"]);
            monHocDTO.HocKy = Convert.ToInt16(dataRow["HocKy"]);
            Guid maMonChinh;
            monHocDTO.MaMonChinh = Guid.TryParse(dataRow["MaMonChinh"].ToString(), out maMonChinh) == true ? ((Guid?)maMonChinh) : null;

            lopMonHocDTO.MonHoc = monHocDTO;

            dangKyGioDayDTO.LopMonHocDTO = lopMonHocDTO;

            dangKyGioDayDTO.MaTietDau = Convert.ToInt32(dataRow["MaTietDau"].ToString());

                TietHocDTO tietHocDau = new TietHocDTO();
                tietHocDau.MaTiet = (int)dangKyGioDayDTO.MaTietDau;
                tietHocDau.TietThu = Convert.ToInt32(dataRow["TietDau"]);
                tietHocDau.GioBatDau = TimeSpan.Parse(dataRow["GioBatDauTietDau"].ToString());
                tietHocDau.GioKetThuc = TimeSpan.Parse(dataRow["GioKetThucTietDau"].ToString());
                tietHocDau.Ca = Convert.ToInt32(    dataRow["CaDau"]);
                tietHocDau.Buoi = Convert.ToBoolean(dataRow["BuoiDau"]);
                dangKyGioDayDTO.TietDauDTO = tietHocDau;

            dangKyGioDayDTO.MaTietSau = Convert.ToInt32(dataRow["MaTietSau"].ToString());

                TietHocDTO tietHocSau = new TietHocDTO();
                tietHocSau.MaTiet = (int)dangKyGioDayDTO.MaTietSau;
                tietHocSau.TietThu = Convert.ToInt32(dataRow["TietSau"]);
                tietHocSau.GioBatDau = TimeSpan.Parse(dataRow["GioBatDauTietSau"].ToString());
                tietHocSau.GioKetThuc = TimeSpan.Parse(dataRow["GioKetThucTietSau"].ToString());
                tietHocSau.Ca = Convert.ToInt32(dataRow["CaSau"]);
                tietHocSau.Buoi = Convert.ToBoolean(dataRow["BuoiSau"]);
                dangKyGioDayDTO.TietSauDTO = tietHocSau;

            dangKyGioDayDTO.Thu = Convert.ToInt16(dataRow["Thu"]);
            dangKyGioDayDTO.MaHocKy = Guid.Parse(dataRow["MaHocKy"].ToString());
            dangKyGioDayDTO.DoUuTien = Convert.ToInt16(dataRow["DoUuTien"]);
            dangKyGioDayDTO.DuocChon = Convert.ToBoolean(dataRow["DuocChon"]);
            dangKyGioDayDTO.Huy = Convert.ToBoolean(dataRow["Huy"]);
            dangKyGioDayDTO.GhiChu = dataRow["GhiChuDK"].ToString();

            return dangKyGioDayDTO;
        }
        #endregion

        #region NguoiDung
        public static NguoiDungDTO ToNguoiDungDTO(this DataRow dataRow)
        {
            NguoiDungDTO nguoidung = new NguoiDungDTO();
            nguoidung.MaNguoiDung = Guid.Parse(dataRow["MaNguoiDung"].ToString());
            nguoidung.TaiKhoanDangNhap = dataRow["TaiKhoanDangNhap"].ToString();
            nguoidung.MatKhau = dataRow["MatKhau"].ToString().Trim();
            nguoidung.VaiTro = Convert.ToInt32(dataRow["VaiTro"].ToString());
            return nguoidung;
        }
        #endregion
        #region SinhVien
        public static SinhVienDTO ToSinhVienDTO(this DataRow dataRow)
        {
            SinhVienDTO sinhvien = new SinhVienDTO();
            sinhvien.ID = Guid.Parse(dataRow["ID"].ToString());
            sinhvien.MaSinhVien = dataRow["MaSinhVien"].ToString();
            sinhvien.HoTenSV = dataRow["HoTenSV"].ToString();
            sinhvien.GioiTinh = (bool)(dataRow["GioiTinh"]) ? 1 : 0;
            sinhvien.NgaySinh = DateTime.Parse(dataRow["NgaySinh"].ToString());
            sinhvien.QueQuan = dataRow["QueQuan"].ToString();
            sinhvien.DiaChi = dataRow["DiaChi"].ToString();
            sinhvien.Email = dataRow["Email"].ToString();
            sinhvien.MaLop = Guid.Parse(dataRow["MaLop"].ToString());
            sinhvien.LopTruong = (bool)dataRow["LopTruong"] ? 1 : 0;
            sinhvien.DuDKLuanVan = (bool)dataRow["DuDKLuanVan"];
            return sinhvien;
        }
        #endregion

        #region TaiLieu
        public static TaiLieuDTO ToTaiLieuDTO(this DataRow dataRow)
        {
            TaiLieuDTO tailieu = new TaiLieuDTO();
            tailieu.MaTaiLieu = Guid.Parse(dataRow["MaTaiLieu"].ToString());
            tailieu.TenTaiLieu = dataRow["TenTaiLieu"].ToString();
            tailieu.TenTapTin = dataRow["TenTapTin"].ToString();
            tailieu.PhanMoRong = dataRow["PhanMoRong"].ToString();
            long kichThuoc;
            tailieu.KichThuoc = Int64.TryParse(dataRow["KichThuoc"].ToString(), out kichThuoc) ? (long?)kichThuoc : null;
            tailieu.MaTapTin = Guid.Parse(dataRow["MaTapTin"].ToString());
            tailieu.MaLoaiTaiLieu = Guid.Parse(dataRow["MaLoaiTaiLieu"].ToString());
            LoaiTaiLieuDTO loaiTaiLieu = new LoaiTaiLieuDTO();
            loaiTaiLieu.MaLoaiTaiLieu = tailieu.MaLoaiTaiLieu;
            loaiTaiLieu.TenLoaiTaiLieu = dataRow["TenLoaiTaiLieu"].ToString();
            tailieu.LoaiTaiLieu = loaiTaiLieu;

            tailieu.MaNguoiTao = Guid.Parse(dataRow["MaNguoiTao"].ToString());
            DateTime ngayTao, ngaySua;
            tailieu.NgayTao = DateTime.TryParse(dataRow["NgayTao"].ToString(), out ngayTao) ? (DateTime?)ngayTao : null;
            tailieu.NgaySua = DateTime.TryParse(dataRow["NgaySua"].ToString(), out ngaySua) ? (DateTime?)ngaySua:null;

            tailieu.MaThuMuc = Guid.Parse(dataRow["MaThuMuc"].ToString());
            int diem;
            tailieu.Diem = Int32.TryParse(dataRow["Diem"].ToString(), out diem) ? (int?) diem : null;
            tailieu.DaKiemDuyet = Convert.ToBoolean(dataRow["DaKiemDuyet"]);
            tailieu.DaXoa = Convert.ToBoolean(dataRow["DaXoa"]);
            tailieu.GhiChu = dataRow["GhiChu"].ToString();
            tailieu.ChiaSe = Convert.ToBoolean(dataRow["ChiaSe"]);

            return tailieu;
        }

        public static LoaiTaiLieuDTO ToLoaiTaiLieuDTO(this DataRow dataRow)
        {
            LoaiTaiLieuDTO loaiTaiLieu = new LoaiTaiLieuDTO();
            loaiTaiLieu.MaLoaiTaiLieu = Guid.Parse(dataRow["MaLoaiTaiLieu"].ToString());
            loaiTaiLieu.TenLoaiTaiLieu = dataRow["TenLoaiTaiLieu"].ToString();
            return loaiTaiLieu;
        }

        public static ThuMucDTO ToThuMucDTO(this DataRow dataRow)
        {
            ThuMucDTO thuMuc = new ThuMucDTO();
            thuMuc.MaThuMuc = Guid.Parse(dataRow["MaThuMuc"].ToString());
            thuMuc.TenThuMuc = dataRow["TenThuMuc"].ToString();
            Guid maThuMucCha;
            thuMuc.MaThuMucCha = Guid.TryParse(dataRow["MaThuMucCha"].ToString(), out maThuMucCha) ? (Guid?)maThuMucCha : null;
            return thuMuc;
        }
        #endregion
        #region Detai
        public static DeTaiDTO ToDeTaiDTO(this DataRow dataRow)
        {
            DeTaiDTO detai = new DeTaiDTO();
            detai.MaDeTai = Guid.Parse(dataRow["MaDeTai"].ToString());
            detai.TenDeTai = dataRow["TenDeTai"].ToString();
            detai.GiangVienHD1 = Guid.Parse(dataRow["GiangVienHD1"].ToString());
            detai.FileTaiLieu = dataRow["FileTaiLieu"].ToString();

            Guid giangVien2Id;
            detai.GiangVienHD2 = Guid.TryParse(dataRow["GiangVienHD2"].ToString(), out giangVien2Id) ? (Guid?)giangVien2Id : null;            
            detai.KhoaLuanTotNghiep = (bool)(dataRow["KhoaLuanTotNghiep"])? 1:0;
            detai.MaHocKy = Guid.Parse(dataRow["MaHocKy"].ToString());
            if (dataRow["MoTa"] != null)
            {
                detai.MoTa = dataRow["MoTa"].ToString();
            }
            else
            {
                detai.MoTa = "";
            }
            detai.DaDuyet = Convert.ToBoolean(dataRow["daDuyet"]);
            detai.DatYeuCau = Convert.ToBoolean(dataRow["DatYeuCau"]);
            detai.GhiChu = dataRow["GhiChu"].ToString();
            return detai;
        }

        public static DeTaiDTO ToDeTaiDTO2(this DataRow dataRow)
        {
            DeTaiDTO detai = new DeTaiDTO();
            detai.MaDeTai = Guid.Parse(dataRow["MaDeTai"].ToString());
            detai.TenDeTai = dataRow["TenDeTai"].ToString();
            detai.GiangVienHD1 = Guid.Parse(dataRow["GiangVienHD1"].ToString());
            detai.FileTaiLieu = dataRow["FileTaiLieu"].ToString();
            Guid giangVien2Id;
            detai.GiangVienHD2 = Guid.TryParse(dataRow["GiangVienHD2"].ToString(), out giangVien2Id) ? (Guid?)giangVien2Id : null;            
            detai.KhoaLuanTotNghiep = (bool)(dataRow["KhoaLuanTotNghiep"])? 1:0;
            detai.MaHocKy = Guid.Parse(dataRow["MaHocKy"].ToString());
            if (dataRow["MoTa"] != null)
            {
                detai.MoTa = dataRow["MoTa"].ToString();
            }
            else
            {
                detai.MoTa = "";
            }
            detai.DaDuyet = Convert.ToBoolean(dataRow["daDuyet"]);
            detai.DatYeuCau = Convert.ToBoolean(dataRow["DatYeuCau"]);
            detai.GhiChu = dataRow["GhiChu"].ToString();

            GiangVienDTO giangVien1 = new GiangVienDTO();
            giangVien1.ID = detai.GiangVienHD1;
            giangVien1.MaGiangVien = dataRow["MaGiangVienHD1"].ToString();
            giangVien1.HoTenGV = dataRow["HoTenGV1"].ToString();
            giangVien1.MaHocVi = Int32.Parse(dataRow["MaHocVi1"].ToString());
            HocViDTO hocVi = new HocViDTO();
            hocVi.MaHocVi = giangVien1.MaHocVi;
            hocVi.TenHocVi = dataRow["TenHocVi1"].ToString();
            hocVi.VietTat = dataRow["HocViVT1"].ToString();
            giangVien1.HocVi = hocVi;
            int maHocHam;
            if (Int32.TryParse(dataRow["MaHocHam1"].ToString(), out maHocHam))
            {
                giangVien1.MaHocHam = maHocHam;
                HocHamDTO hocHam = new HocHamDTO();
                hocHam.MaHocHam = maHocHam;
                hocHam.TenHocHam = dataRow["TenHocHam1"].ToString();
                hocHam.VietTat = dataRow["HocHamVT1"].ToString();
                giangVien1.HocHam = hocHam;
            }
            else
            {
                giangVien1.MaHocHam = null;
                giangVien1.HocHam = null;
            }

            detai.GiangVien1 = giangVien1;

            if (detai.GiangVienHD2 != null)
            {
                GiangVienDTO giangVien2 = new GiangVienDTO();
                giangVien2.ID = (Guid)detai.GiangVienHD2;
                giangVien2.MaGiangVien = dataRow["MaGiangVienHD2"].ToString();
                giangVien2.HoTenGV = dataRow["HoTenGV2"].ToString();
                giangVien2.MaHocVi = Int32.Parse(dataRow["MaHocVi2"].ToString());
                HocViDTO hocVi2 = new HocViDTO();
                hocVi2.MaHocVi = giangVien2.MaHocVi;
                hocVi2.TenHocVi = dataRow["TenHocVi2"].ToString();
                hocVi2.VietTat = dataRow["HocViVT2"].ToString();
                giangVien2.HocVi = hocVi2;
                int maHocHam2;
                if (Int32.TryParse(dataRow["MaHocHam2"].ToString(), out maHocHam))
                {
                    giangVien2.MaHocHam = maHocHam;
                    HocHamDTO hocHam = new HocHamDTO();
                    hocHam.MaHocHam = maHocHam;
                    hocHam.TenHocHam = dataRow["TenHocHam2"].ToString();
                    hocHam.VietTat = dataRow["HocHamVT2"].ToString();
                    giangVien2.HocHam = hocHam;
                }
                else
                {
                    giangVien2.MaHocHam = null;
                    giangVien2.HocHam = null;
                }
                detai.GiangVien2 = giangVien2;
            }
            return detai;
        }
        #endregion
        #region DangKyDeTai
        public static DangKyDeTaiDTO ToDangKyDeTaiDTO(this DataRow dataRow)
        {
            DangKyDeTaiDTO dangkydetai = new DangKyDeTaiDTO();
            dangkydetai.MaDangKyDeTai = Guid.Parse(dataRow["MaDangKyDeTai"].ToString());
            dangkydetai.MaDeTai = Guid.Parse(dataRow["MaDeTai"].ToString());            
            dangkydetai.MaNamHoc = Guid.Parse(dataRow["MaNamHoc"].ToString());
            dangkydetai.SinhVienDangKy = Guid.Parse(dataRow["SinhVienDangKy"].ToString());
            dangkydetai.MaSinhVien1 = dataRow["MaSinhVien1"].ToString();
            dangkydetai.MaSinhVien2 = dataRow["MaSinhVien2"].ToString();
            dangkydetai.NgayDangKy = DateTime.Parse(dataRow["NgayDangKy"].ToString());
            return dangkydetai;
        }
        #endregion
        #region BanTin
        public static BanTinDTO ToBanTinDTO(this DataRow dataRow)
        {
            BanTinDTO bantin = new BanTinDTO();
            bantin.ID = Guid.Parse(dataRow["ID"].ToString());
            bantin.TieuDe = dataRow["TieuDe"].ToString();
            bantin.NoiDung = dataRow["NoiDung"].ToString();
            bantin.NgayThem = DateTime.Parse(dataRow["NgayThem"].ToString());
            bantin.QuyenTruyCap = int.Parse(dataRow["QuyenTruyCap"].ToString());
            return bantin;
        }
        #endregion
        #region ThongBao
        public static ThongBaoDTO ToThongBaoDTO(this DataRow dataRow)
        {
            ThongBaoDTO thongbao = new ThongBaoDTO();
            thongbao.MaThongBao = Guid.Parse(dataRow["MaThongBao"].ToString());
            thongbao.TieuDeThongBao = dataRow["TieuDeThongBao"].ToString();
            thongbao.NoiDungThongBao = dataRow["NoiDungThongBao"].ToString();
            thongbao.NgayGuiThongBao = DateTime.Parse(dataRow["NgayGuiThongBao"].ToString());
            Guid nguoiGuiId;
            thongbao.NguoiGuiId = Guid.TryParse(dataRow["NguoiGui"].ToString(), out nguoiGuiId) ? (Guid?)nguoiGuiId : null;
            Guid maTaiLieu;
            thongbao.MaTaiLieu1 = Guid.TryParse(dataRow["MaTaiLieu1"].ToString(), out maTaiLieu) ? (Guid?) maTaiLieu : null;
            thongbao.MaTaiLieu2 = Guid.TryParse(dataRow["MaTaiLieu2"].ToString(), out maTaiLieu) ? (Guid?) maTaiLieu : null;
            thongbao.MaTaiLieu3 = Guid.TryParse(dataRow["MaTaiLieu3"].ToString(), out maTaiLieu) ? (Guid?) maTaiLieu : null;
            thongbao.MaTaiLieu4 = Guid.TryParse(dataRow["MaTaiLieu4"].ToString(), out maTaiLieu) ? (Guid?) maTaiLieu : null;
            thongbao.MaTaiLieu5 = Guid.TryParse(dataRow["MaTaiLieu5"].ToString(), out maTaiLieu) ? (Guid?) maTaiLieu : null;

            if (nguoiGuiId != null)
            {
                GiangVienDTO giangvien = new GiangVienDTO();
                giangvien.ID = (Guid)thongbao.NguoiGuiId;
                giangvien.HoTenGV = dataRow["HoTenGV"].ToString();
                giangvien.MaHocVi = Convert.ToInt32(dataRow["MaHocVi"]);
                HocViDTO hocviDTO = new HocViDTO();
                hocviDTO.MaHocVi = giangvien.MaHocVi;
                hocviDTO.TenHocVi = dataRow["TenHocVi"].ToString();
                hocviDTO.VietTat = dataRow["HocViVT"].ToString();
                giangvien.HocVi = hocviDTO;
                int maHocHam;
                giangvien.MaHocHam = Int32.TryParse(dataRow["MaHocHam"].ToString(), out maHocHam) ? (int?)maHocHam : null;

                if (giangvien.MaHocHam != null)
                {
                    HocHamDTO hocHam = new HocHamDTO();
                    hocHam.MaHocHam = (int)giangvien.MaHocHam;
                    hocHam.TenHocHam = dataRow["TenHocHam"].ToString();
                    hocHam.VietTat = dataRow["HocHamVT"].ToString();
                    giangvien.HocHam = hocHam;
                }
                else
                    giangvien.HocHam = null;
                thongbao.NguoiGui = giangvien;
            }
            else
                thongbao.NguoiGui = null;

            return thongbao;
        }
        #endregion

        #region ChiTietThongBao
        public static ChiTietThongBaoDTO ToChiTietThongBaoDTO(this DataRow dataRow)
        {
            ChiTietThongBaoDTO chitiet = new ChiTietThongBaoDTO();
            chitiet.MaChiTietThongBao = Guid.Parse(dataRow["MaChiTietThongBao"].ToString());
            chitiet.MaThongBao = Guid.Parse(dataRow["MaThongBao"].ToString());
            chitiet.MaNguoiNhan = Guid.Parse(dataRow["MaNguoiNhan"].ToString());
            chitiet.DaXem = (bool)(dataRow["DaXem"]) ? 1 : 0;

            ThongBaoDTO thongbao = new ThongBaoDTO();
            thongbao.MaThongBao = Guid.Parse(dataRow["MaThongBao"].ToString());
            thongbao.TieuDeThongBao = dataRow["TieuDeThongBao"].ToString();
            thongbao.NoiDungThongBao = dataRow["NoiDungThongBao"].ToString();
            thongbao.NgayGuiThongBao = DateTime.Parse(dataRow["NgayGuiThongBao"].ToString());

            Guid nguoiGuiId;
            thongbao.NguoiGuiId = Guid.TryParse(dataRow["NguoiGui"].ToString(), out nguoiGuiId) ? (Guid?)nguoiGuiId : null;

            if (thongbao.NguoiGuiId != null)
            {
                GiangVienDTO giangVien = new GiangVienDTO();
                giangVien.ID = (Guid)thongbao.NguoiGuiId;
                giangVien.HoTenGV = dataRow["HoTenGV"].ToString();
                giangVien.Email = dataRow["Email"].ToString();
                thongbao.NguoiGui = giangVien;
            }
            else
                thongbao.NguoiGui = null;

            chitiet.ThongBao = thongbao;

            return chitiet;
        }
        //public static ChiTietThongBaoDTO ToChiTietThongBao2DTO(this DataRow dataRow)
        //{
        //    ChiTietThongBaoDTO chitiet = new ChiTietThongBaoDTO();
        //    chitiet.MaChiTietThongBao = Guid.Parse(dataRow["MaChiTietThongBao"].ToString());
        //    chitiet.MaThongBao = Guid.Parse(dataRow["MaThongBao"].ToString());
        //    chitiet.MaNguoiNhan = Guid.Parse(dataRow["MaNguoiNhan"].ToString());
        //    chitiet.DaXem = (bool)(dataRow["DaXem"]) ? 1 : 0;

        //    //ThongBaoDTO thongbao = new ThongBaoDTO();
        //    //thongbao.MaThongBao = Guid.Parse(dataRow["MaThongBao"].ToString());
        //    //thongbao.TieuDeThongBao = dataRow["TieuDeThongBao"].ToString();
        //    //thongbao.NoiDungThongBao = dataRow["NoiDungThongBao"].ToString();
        //    //thongbao.NgayGuiThongBao = DateTime.Parse(dataRow["NgayGuiThongBao"].ToString());

        //    //chitiet.ThongBao = thongbao;

        //    return chitiet;
        //}
        #endregion

        #region BangDiem
        public static BangDiemDTO ToBangDiemDTO(this DataRow dataRow)
        {
            BangDiemDTO bangdiem = new BangDiemDTO();
            bangdiem.MaBangDiem = Guid.Parse(dataRow["MaBangDiem"].ToString());
            bangdiem.SinhVienId = Guid.Parse(dataRow["SinhVienId"].ToString());
            bangdiem.MaMonHoc = Guid.Parse(dataRow["MaMonHocID"].ToString());
            bangdiem.MaHocKy = Guid.Parse(dataRow["MaHocKy"].ToString());
            bangdiem.DiemHocPhan = Int32.Parse(dataRow["DiemHocPhan"].ToString());

            MonHocDTO monhoc = new MonHocDTO();
            monhoc.MaMonHoc = dataRow["MaMonHoc"].ToString();
            monhoc.TenMonHoc = dataRow["TenMonHoc"].ToString();
            monhoc.TongSoTinChi = short.Parse(dataRow["SoTinChi"].ToString());
            monhoc.SoTinChiLyThuyet = short.Parse(dataRow["SoTinChiLyThuyet"].ToString());
            monhoc.HocKy = short.Parse(dataRow["HocKy"].ToString());

            bangdiem.MonHoc = monhoc;

            HocKyDTO hocky = new HocKyDTO();
            hocky.MaNamHoc = Guid.Parse(dataRow["MaNamHoc"].ToString());
            hocky.TenHocKy = int.Parse(dataRow["TenHocKy"].ToString());

            bangdiem.HocKy = hocky;

            NamHocDTO namhoc = new NamHocDTO();
            namhoc.Nam1 = int.Parse(dataRow["Nam1"].ToString());
            namhoc.Nam2 = int.Parse(dataRow["Nam2"].ToString());

            bangdiem.NamHoc = namhoc;

            return bangdiem;
        }
        public static BangDiemDTO ToBangDiem2DTO(this DataRow dataRow)// just for insert and update
        {
            BangDiemDTO bangdiem = new BangDiemDTO();
            bangdiem.MaBangDiem = Guid.Parse(dataRow["MaBangDiem"].ToString());
            bangdiem.SinhVienId = Guid.Parse(dataRow["SinhVienId"].ToString());
            bangdiem.MaMonHoc = Guid.Parse(dataRow["MaMonHocID"].ToString());
            bangdiem.MaHocKy = Guid.Parse(dataRow["MaHocKy"].ToString());
            bangdiem.DiemHocPhan = Int32.Parse(dataRow["DiemHocPhan"].ToString());            

            return bangdiem;
        }
        #endregion
        #region  DangKyKetThucLopMonHoc
        public static DangKyKetThucLopMonHocDTO ToDangKyKetThucLopMonHocDTO(this DataRow dataRow)
        {
            DangKyKetThucLopMonHocDTO dangky = new DangKyKetThucLopMonHocDTO();
            dangky.ID = Guid.Parse(dataRow["ID"].ToString());
            dangky.GiangVienID = Guid.Parse(dataRow["GiangVienID"].ToString());
            dangky.MaLopMonHoc = dataRow["MaLopMonHoc"].ToString();
            dangky.NgayKetThucLopMonHoc = DateTime.Parse(dataRow["NgayKetThucLopMonHoc"].ToString());
            dangky.MaHocKy = Guid.Parse(dataRow["MaHocKy"].ToString());
            dangky.NgayDangKy = DateTime.Parse(dataRow["NgayDangKy"].ToString());

            MonHocDTO monhoc = new MonHocDTO();
            monhoc.TenMonHoc = dataRow["TenMonHoc"].ToString();
            dangky.MonHoc = monhoc;

            return dangky;
        }
        public static DangKyKetThucLopMonHocDTO ToDangKyKetThucLopMonHoc2DTO(this DataRow dataRow)
        {
            DangKyKetThucLopMonHocDTO dangky = new DangKyKetThucLopMonHocDTO();
            dangky.ID = Guid.Parse(dataRow["ID"].ToString());
            dangky.GiangVienID = Guid.Parse(dataRow["GiangVienID"].ToString());
            dangky.MaLopMonHoc = dataRow["MaLopMonHoc"].ToString();
            dangky.NgayKetThucLopMonHoc = DateTime.Parse(dataRow["NgayKetThucLopMonHoc"].ToString());
            dangky.MaHocKy = Guid.Parse(dataRow["MaHocKy"].ToString());

            //MonHocDTO monhoc = new MonHocDTO();
            //monhoc.TenMonHoc = dataRow["TenMonHoc"].ToString();
            //dangky.MonHoc = monhoc;

            return dangky;
        }
        #endregion

        #region Lich Thi
        public static LichThiDTO ToLichThiDTO(this DataRow dataRow)
        {
            try
            {
                LichThiDTO lichThiDTO = new LichThiDTO();
                lichThiDTO.MaLichThi = Guid.Parse(dataRow["MaLichThi"].ToString());
                lichThiDTO.MaLopMonHoc = Guid.Parse(dataRow["lopMonHocId"].ToString());
                LopMonHocDTO lopMonHoc = new LopMonHocDTO();
                lopMonHoc = dataRow.ToLopMonHocDTO();
                lichThiDTO.LopMonHoc = lopMonHoc;

                lichThiDTO.NgayGio = Convert.ToDateTime(dataRow["NgayGio"]);
                lichThiDTO.ThoiGianLamBai = Int16.Parse(dataRow["ThoiGianLamBai"].ToString());
                lichThiDTO.HinhThucThi = Int32.Parse(dataRow["HinhThucThi"].ToString());
                lichThiDTO.GhiChu = dataRow["GhiChuLichThi"].ToString();
                return lichThiDTO;
            }
            catch
            {
                return null;
            }
        }
        #endregion
        #region
        public static BauChonDeTaiDTO ToBauChonDeTaiDTO(this DataRow dataRow)
        {
            BauChonDeTaiDTO bauChonDeTaiDTO = new BauChonDeTaiDTO();
            DeTaiDTO deTaiDTO = new DeTaiDTO();

            deTaiDTO.MaDeTai = Guid.Parse(dataRow["MaDeTai"].ToString());
            deTaiDTO.TenDeTai = dataRow["TenDeTai"].ToString();
            deTaiDTO.MoTa = dataRow["MoTa"].ToString();
            deTaiDTO.SoLuotBauChon = Int32.Parse(dataRow["SoLuotBauChon"].ToString());
            deTaiDTO.FileTaiLieu = dataRow["FileTaiLieu"].ToString();
            bauChonDeTaiDTO.DeTai = deTaiDTO;
            
            bauChonDeTaiDTO.HoTenGV1 = (dataRow["HoTenGV1"]!=null)? dataRow["HoTenGV1"].ToString() : "";
            bauChonDeTaiDTO.HoTenGV2 = (dataRow["HoTenGV2"] != null) ? dataRow["HoTenGV2"].ToString() : "";
            bauChonDeTaiDTO.HoTenSV1 = (dataRow["HoTenSV1"] != null) ? dataRow["HoTenSV1"].ToString() : "";
            bauChonDeTaiDTO.HoTenSV2 = (dataRow["HoTenSV2"] != null) ? dataRow["HoTenSV2"].ToString() : "";
            bauChonDeTaiDTO.MaSV1 = (dataRow["MaSV1"] != null) ? dataRow["MaSV1"].ToString() : "";
            bauChonDeTaiDTO.MaSV2 = (dataRow["MaSV2"] != null) ? dataRow["MaSV2"].ToString() : "";
            bauChonDeTaiDTO.TenHocKy = (dataRow["TenHocKy"] != null) ? dataRow["TenHocKy"].ToString() : "";
            

            bauChonDeTaiDTO.Nam1 = Int32.Parse(dataRow["Nam1"].ToString());
            bauChonDeTaiDTO.Nam2 = Int32.Parse(dataRow["Nam2"].ToString());

            return bauChonDeTaiDTO;
        }
        #endregion
        #region Tien do de tai
        public static TienDoDeTaiDTO ToTienDoDeTaiDTO(this DataRow dataRow)
        {
            TienDoDeTaiDTO tienDoDTO = new TienDoDeTaiDTO();
            tienDoDTO.MaTienDoDeTai = Guid.Parse(dataRow["MaTienDoDeTai"].ToString());
            tienDoDTO.MaDeTai = Guid.Parse(dataRow["MaDeTai"].ToString());
            tienDoDTO.NgayBaoCao = DateTime.Parse(dataRow["NgayBaoCao"].ToString());
            tienDoDTO.NoiDung = dataRow["NoiDung"].ToString();
            tienDoDTO.TieuDe = dataRow["TieuDe"].ToString();
            tienDoDTO.TienDoHoanThanh = Int32.Parse(dataRow["TienDoHoanThanh"].ToString());

            DeTaiDTO deTaiDTO = new DeTaiDTO();
            deTaiDTO.TenDeTai = dataRow["TenDeTai"].ToString();
            deTaiDTO.MaHocKy = Guid.Parse(dataRow["MaHocKy"].ToString());

            tienDoDTO.DeTai = deTaiDTO;
            return tienDoDTO;
        }
        #endregion
    }
}
